package com.decent.ejfadmin.product.controller;

import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupplyProduct;
import com.decent.ejfadmin.common.exception.SupException;
import com.decent.ejfadmin.product.request.ProductQueryParam;
import com.decent.ejfadmin.product.request.SetUpModelPriceParam;
import com.decent.ejfadmin.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangchao
 * @date 2018/7/25 15:48
 */
@Slf4j
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 查询产品列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "findProductListPage")
    public Page findProductListPage(ProductQueryParam param) {
        Page page = new Page();
        try {
            page = productService.findProductListPage(param);
        } catch (Exception e) {
            page.setTotal(0);
            log.warn("ProductController findProductListPage occurs error Internal Server Error  ", e);
        }
        return page;
    }

    /**
     * 修改产品上下架
     *
     * @param productIds
     * @param status
     * @param remark
     * @return
     */
    @RequestMapping(value = "changeProductStatus", method = RequestMethod.POST)
    public MessageBean changeProductStatus(String productIds, Integer status, String remark) {
        MessageBean messageBean = MessageBean.success();
        try {
            if (StringUtils.isBlank(remark)) {
                return MessageBean.fail("备注不能为空！");
            }
            productService.changeProductStatus(productIds, status, remark);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("ProductController changeProductStatus occurs error " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ProductController changeProductStatus occurs error Internal Server Error  ", e);
        }
        return messageBean;
    }

    /**
     * 快速修改产品状态
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "setProductStatus", method = RequestMethod.POST)
    public MessageBean setProductStatus(SetUpModelPriceParam param) {
        MessageBean messageBean = MessageBean.success();
        try {
            productService.setProductStatus(param);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("ProductController setProductStatus occurs error Internal Server Error  " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ProductController setProductStatus occurs error Internal Server Error  " + e.getMessage());
        }
        return messageBean;
    }

    /**
     * 新增产品
     *
     * @param product
     * @return
     */
    @RequestMapping("addProduct")
    public MessageBean addProduct(SupplyProduct product) {
        try {
            return productService.addProduct(product);
        } catch (Exception e) {
            log.warn("新增产品异常[{}][{}]", product, e.getLocalizedMessage(), e);
            String duplicate = "Duplicate entry";
            if (e.getLocalizedMessage().contains(duplicate)) {
                return MessageBean.fail(product.getCardProductId() + "产品编码已存在");
            }
            return MessageBean.fail();
        }
    }

    /**
     * 修改产品
     *
     * @param product
     * @return
     */
    @RequestMapping("editProduct")
    public MessageBean editProduct(SupplyProduct product) {
        try {
            return productService.editProduct(product);
        } catch (Exception e) {
            log.warn("修改产品异常[{}][{}]", product, e.getLocalizedMessage(), e);
            String duplicate = "Duplicate entry";
            if (e.getLocalizedMessage().contains(duplicate)) {
                return MessageBean.fail(product.getCardProductId() + "产品编码已存在");
            }
            return MessageBean.fail();
        }
    }

    /**
     * 编辑产品校验规则
     *
     * @param productIds   产品id
     * @param cardRestrict 校验规则
     * @return
     */
    @RequestMapping("updateCardRestrict")
    public MessageBean updateCardRestrict(String productIds, String cardRestrict) {
        try {
            return productService.updateCardRestrict(productIds, cardRestrict);
        } catch (Exception e) {
            log.warn("编辑产品[{}]校验规则[{}]异常[{}]", productIds, cardRestrict, e.getLocalizedMessage(), e);
            return MessageBean.fail();
        }
    }

    /**
     * 删除产品
     *
     * @param productIds 产品id
     * @return
     */
    @RequestMapping("deleteProduct")
    public MessageBean deleteProduct(String productIds) {
        try {
            return productService.deleteProduct(productIds);
        } catch (Exception e) {
            log.warn("删除产品[{}]异常[{}]", productIds, e.getLocalizedMessage(), e);
            return MessageBean.fail();
        }
    }
}
